package 力扣日常刷题.木23一月.第01天301;

import 力扣日常刷题.TreeNode;

import java.util.HashMap;
import java.util.TreeMap;

/**
 * @author 帅小伙
 * @date 2022/3/1
 * @description
 */
public class Demo03重建二叉树 {

    int[] preorder;
    HashMap<Integer, Integer>dic = new HashMap<>();
    public TreeNode buildTree(int[] preorder,int[] inorder) {
        this.preorder = preorder;
        for (int i = 0; i < preorder.length; i++) {
            dic.put(inorder[i],i);
        }
        return recur(0,0,inorder.length - 1);
    }

    public TreeNode recur(int root,int l,int r) {
        if(l > r) return null;
        TreeNode treeNode = new TreeNode(preorder[root]);
        int i = dic.get(preorder[root]);
        treeNode.left = recur(root+1,l,i - 1);
        treeNode.right = recur(root+i - l +1 , i + 1,r);
        return treeNode;
    }



}
